/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sistema.aplicacao;

import java.util.Comparator;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

/**
 *
 * @author gr
 */
public class OrdenaColunaJTable
{

    public static void ordenaTabela( int colunas[], DefaultTableModel modelo, JTable tabela )
    {
        //comparador para ordenar tabela
        Comparator<String> comparator2 = new Comparator<String>()
        {

            public int compare( String s1, String s2 )
            {
                Double valor1 = null;
                Double valor2 = null;
                try
                {
                    valor1 = Double.parseDouble( s1 );
                    valor2 = Double.parseDouble( s2 );
                } catch ( Exception e )
                {
                    e.printStackTrace();
                }

                return valor1.compareTo( valor2 );
            }
        };


        //Ordena as colunas da tabela
        TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>( modelo );

        for ( int x = 0; x < colunas.length; x++ )
        {
            sorter.setComparator( colunas[x], comparator2 );
        }

        tabela.setRowSorter( sorter );

    }
}
